package com.ziq.zcamp.view

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import com.ziq.base.extension.dp


class FastIndexView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {
    var dataList = mutableListOf<String>("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K",
        "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "#")

    private var cellWidth = 15.dp.toFloat()
    private var cellHeight = 15.dp.toFloat()
    private var contentHeight = 0f

    private var mTextPaint: Paint = Paint()
    private var mBackgroundPaint: Paint = Paint()
    private var textColor = Color.parseColor("#6E5F55")
    private var selectTextColor = Color.parseColor("#F6F6F6")
    private var selectBackgroundColor = Color.parseColor("#AF8B6C")
    private var touchIndex = 0

    private var lastY = 0f
    private var touchYinContent = 0f

    var onSelectedListener : ((index:Int, text:String)->Unit)? = null
    var onEventListener : ((text:String, action:Int, actionCenterY:Float)->Unit)? = null

    init {
        mTextPaint.textSize = 10.dp.toFloat()
        mTextPaint.isAntiAlias = true
        contentHeight = cellHeight * dataList.size
        mBackgroundPaint.setColor(selectBackgroundColor)
    }

    fun setData(data :List<String>){
        dataList.clear()
        dataList.addAll(data)
        contentHeight = cellHeight * dataList.size
        requestLayout()
    }

    fun updateUISetting(textColor:Int, selectTextColor:Int, selectBackgroundColor:Int){
        this.textColor = textColor
        this.selectTextColor = selectTextColor
        this.selectBackgroundColor = selectBackgroundColor
        mBackgroundPaint.setColor(selectBackgroundColor)
    }


    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
    }

    override fun onDraw(canvas: Canvas) {
        for ((index, item) in dataList.withIndex()){
            //计算绘制字符的X方向起点
            val x = (width / 2.0f - mTextPaint.measureText(item) / 2.0f)
            val bounds = Rect()
            mTextPaint.getTextBounds(item, 0, item.length, bounds)
            val textHeight = bounds.height()
            //计算绘制字符的Y方向起点
            val ycenter = cellHeight / 2.0f + (index * cellHeight)
            val y = ycenter + textHeight / 2.0f
            if(touchIndex == index){
                mTextPaint.setColor(selectTextColor)
                canvas.drawCircle(width / 2.0f, ycenter, cellHeight/2.0f, mBackgroundPaint)
            } else {
                mTextPaint.setColor(textColor)
            }
            canvas.drawText(item, x, y, mTextPaint)
        }
    }

    override fun onTouchEvent(event: MotionEvent): Boolean {
        val index: Int
        when (event.action) {
            MotionEvent.ACTION_DOWN -> {
                //计算当前触摸的字符索引
                touchYinContent = event.y + scrollY.toFloat()
                index = (touchYinContent / cellHeight).toInt()
                if (index >= 0 && index < dataList.size) {
                    setTouchIndexInner(index, MotionEvent.ACTION_DOWN)
                }
                lastY = event.y
            }
            MotionEvent.ACTION_MOVE -> {
                //计算当前触摸的字符索引
                touchYinContent = event.y + scrollY.toFloat()
                index = (touchYinContent / cellHeight).toInt()
                var dy = lastY - event.y // 小于0 往下滑
                if (index >= 0 && index < dataList.size) {
                    if(height > 0 && contentHeight > height){
                        var targetScrollY = scrollY - dy.toInt()
                        if(targetScrollY < 0){
                            targetScrollY = 0
                        } else if(targetScrollY >= contentHeight - height){
                            targetScrollY = (contentHeight - height).toInt()
                        }
                        scrollY = targetScrollY
                    }
                    setTouchIndexInner(index, MotionEvent.ACTION_MOVE)
                }
                lastY = event.y
            }
            MotionEvent.ACTION_UP -> {
                sendEvent(MotionEvent.ACTION_UP)
            }
        }
        return true
    }

    fun setTouchIndex(index:Int){
        if (index >= 0 && index < dataList.size) {
            if (index != touchIndex) {
                touchIndex = index
                invalidate()
            }
        }
    }

    fun setTouchGroup(name:String){
        if(dataList.contains(name)){
            val index = dataList.indexOf(name)
            if (index != touchIndex) {
                touchIndex = index
                invalidate()
            }
        }
    }

    private fun setTouchIndexInner(index:Int, action:Int){
        if (index != touchIndex) {
            touchIndex = index
            invalidate()
            onSelectedListener?.invoke(touchIndex, dataList.get(touchIndex))
        }
        sendEvent(action)
//        IKLog.d("ziq", "FastIndexView ${scrollY} ${centerY} ${dataList.get(touchIndex)}")
    }

    private fun sendEvent(action:Int){
        val centerY = cellHeight / 2.0f + (touchIndex * cellHeight) - scrollY
        var text = ""
        if (touchIndex >= 0 && touchIndex < dataList.size) {
            text = dataList.get(touchIndex)
        }
        onEventListener?.invoke(text, action, centerY)
    }


}